/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * libburmese - Burmese Language Support Library
 * Copyright (C) 2011  Thura Hlaing <trhura@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * myctype.c - Burmese Unicode character classifications
 * Created: 2011-01-07
 */

#include <burmese/mystr.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>

void delete_event_cb (GtkWidget *widget, GdkEvent *event, gpointer data);
void sbrkbtn_clicked_cb (GtkToolButton *button, gpointer data);

int main (int argc, char *argv[])
{
    setlocale(LC_CTYPE, "");
    gtk_init (&argc, &argv);

    /* Create the main window */
    GtkWidget *mainwin, *mainbox, *toolbar;
    GtkWidget *textbox, *ltextview, *rtextview;

    ltextview = gtk_text_view_new ();
    rtextview = gtk_text_view_new ();
    gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (ltextview), GTK_WRAP_CHAR);
    gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (rtextview), GTK_WRAP_CHAR);
    gtk_text_view_set_editable (GTK_TEXT_VIEW (rtextview), FALSE);
    gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (rtextview), FALSE);

    textbox = gtk_hbox_new (TRUE, 2);
    gtk_box_pack_start (GTK_BOX(textbox), ltextview, TRUE, TRUE, 0);
    gtk_box_pack_start (GTK_BOX(textbox), rtextview, TRUE, TRUE, 0);

    toolbar = gtk_toolbar_new ();
    GtkToolItem *sbrkbtn = gtk_tool_button_new (NULL, "Syllable Break");
    GtkToolItem *wbrkbtn = gtk_tool_button_new (NULL, "Word Break");

    gtk_tool_button_set_stock_id (GTK_TOOL_BUTTON(sbrkbtn), GTK_STOCK_CONVERT);
    gtk_tool_button_set_stock_id (GTK_TOOL_BUTTON(wbrkbtn), GTK_STOCK_CONVERT);

    gtk_toolbar_set_style (GTK_TOOLBAR(toolbar), GTK_TOOLBAR_BOTH);
    gtk_toolbar_insert (GTK_TOOLBAR(toolbar), wbrkbtn, -1);
    gtk_toolbar_insert (GTK_TOOLBAR(toolbar), sbrkbtn, 0);

    mainbox = gtk_vbox_new (FALSE, 5);
    gtk_box_pack_start (GTK_BOX(mainbox), toolbar, FALSE, FALSE, 2);
    gtk_box_pack_end (GTK_BOX(mainbox), textbox, TRUE, TRUE, 2);

    mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_container_add (GTK_CONTAINER(mainwin), mainbox);
    gtk_container_set_border_width (GTK_CONTAINER(mainwin), 3);
    gtk_window_resize (GTK_WINDOW(mainwin), 800, 600);
    gtk_widget_show_all (mainwin);

    GtkWidget* textviews[] = { ltextview, rtextview };

    g_signal_connect (mainwin, "delete-event", G_CALLBACK(delete_event_cb), NULL);
    g_signal_connect (sbrkbtn, "clicked", G_CALLBACK(sbrkbtn_clicked_cb), textviews);
    gtk_main ();

    return 0;
}

void delete_event_cb (GtkWidget *widget, GdkEvent *event, gpointer data)
{
    gtk_main_quit ();
}

void sbrkbtn_clicked_cb (GtkToolButton *button, gpointer data)
{
    GtkWidget **textviews = (GtkWidget **) data;
    GtkWidget *ltextview = textviews[0];
    GtkWidget *rtextview = textviews[1];

    GtkTextBuffer *buffer;
    gchar *itext = (gchar *) alloca (sizeof (gchar) * 1024);

    buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (ltextview));
    g_object_get (buffer, "text", &itext, NULL);

    gunichar *wtext     = g_utf8_to_ucs4_fast (itext, -1, NULL);
    wchar_t  *iwtext    = (wchar_t*) wtext;
    wchar_t  *cluster  = NULL;
    wchar_t  *otext     = (wchar_t*) alloca (sizeof (wchar_t) * 1024);
    wchar_t  *iotext    = otext;

    while (iwtext != NULL) {
        iwtext = mystr_next_cluster (iwtext, &cluster);
        if (cluster) {
            wcscpy (iotext, cluster);

            size_t length = wcslen (cluster);
            iotext[length] = L'+';
            iotext[length+1] = L'\0';

            iotext = iotext+length+1;
            free (cluster);
        }
    }

//    printf ("%ls %d\n", otext, wcslen (otext));
    otext[wcslen(otext)-1] = L'\0';

    buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (rtextview));
    gchar *text = g_ucs4_to_utf8 ((gunichar *) otext, -1, NULL, NULL, NULL);

    if (text) {
        g_object_set (buffer, "text", text, NULL);
        g_free (text);
    }
}
